package process;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

import abstractclass.AbstractProcess;
import annotation.Caption;
import freemarker.template.Template;
import util.StringUilts;

public class MServiceProcess  extends AbstractProcess{
	
	@Override
	public void processJava(String packgerOutPath, String beanName, String path,Object object) {
		if(path==null){
			throw new NullPointerException("path is null");
		}
		path = path+"api"+File.separator;
		File file = new File(path);
		if(!file.exists()){
			file.mkdirs();
		}
		try 
        {
			Template template = configuration.getTemplate(ftlFilePrefix+"mservice.ftl");
			Map<String, Object> ParamMap = new HashMap<String, Object>();
            Map<String,Object> listCaptions = new  HashMap<String, Object>();

			
			Map<String, Object> fields = new HashMap<String, Object>();
			 Field[] fs =  object.getClass().getDeclaredFields();
	            for (Field f : fs) {
	            	Caption caption = f.getAnnotation(Caption.class);
	            	fields.put( f.getName(), StringUilts.getFisrtUpperCaseChar(f.getName()));
	            	if(null!=caption){
	            		listCaptions.put(f.getName(), caption.value());
	            	}else{
	            		listCaptions.put(f.getName(),"");
	            	}
				}
            ParamMap.put("packageOutPath", packgerOutPath);
            ParamMap.put("bean", beanName);
            ParamMap.put("fields", fields);
            ParamMap.put("listCaptions", listCaptions);
            System.out.println(fields.toString());
            System.out.println(listCaptions.toString());
            Writer writer  = new OutputStreamWriter(new FileOutputStream(path+"M"+beanName+"Service.java"),"UTF-8"); 
            template.process(ParamMap, writer);  
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	

}
